﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using DevComponents.DotNetBar;
using DAL;
using BLL;
using BOL;

namespace QLThueMuonXe
{
    public partial class frmThongTinNhanVien : UserControl
    {
        string imagename;//lưu tên của hình
        FormNhanVien fnv = new FormNhanVien();
        public string manv;
        string matkhau;

        public frmThongTinNhanVien()
        {
            InitializeComponent();
        }

        #region Điều khiển đóng/mở form
        private HamDungChung _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public HamDungChung Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    HamDungChung oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(HamDungChung oldValue, HamDungChung newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmttnvien;
            }
            else
            {
                btndong.Command = null;
            }
        }
        #endregion


        private void btnduyetdanh_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog dldlg = new OpenFileDialog();
                dldlg.InitialDirectory = @":d\";
                dldlg.Filter = "Image File(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (dldlg.ShowDialog() == DialogResult.OK)
                {
                    imagename = dldlg.FileName;
                    Bitmap newimg = new Bitmap(imagename);
                    picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                    picanhdaidien.Image = (Image)newimg;
                }
                dldlg = null;

            }
            catch (System.ArgumentException ae)
            {
                imagename = " ";
                MessageBox.Show(ae.Message.ToString());

            }
        }

        //khi chưa click sửa
        private void AnHien(bool a)
        {
            txtdiachi.Enabled = a;
            txtten.Enabled = a;
            dateTimeInput1.Enabled = a;
            cobgioitinh.Enabled = a;
            btnduyetdanh.Enabled = a;            
            txtsdt.Enabled = a;
        }

        public void loadx()
        {
            AnHien(false);
            var ttnv = fnv.LayDSNhanVien1(manv).FirstOrDefault();
            lblmanv.Text = ttnv.manv;
            txtten.Text = ttnv.hotennv;
            txtdiachi.Text = ttnv.diachinv;
            cobgioitinh.Text = ttnv.gioitinhnv;
            dateTimeInput1.Value = ttnv.ngaysinhnv;
            txtsdt.Text = ttnv.sdtnv;
            txtchucvu.Text = ttnv.chucvu;
            //load ảnh
            Image img = Image.FromStream(ttnv.hinhanh);
            if (img == null) return;
            picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
            picanhdaidien.Image = img;

            matkhau = ttnv.matkhau;
        }

        private void frmThongTinNhanVien_Load(object sender, EventArgs e)
        {
            cobgioitinh.Items.Add("Nam");
            cobgioitinh.Items.Add("Nữ");
            cobgioitinh.Items.Add("Khác");
            loadx();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            AnHien(true);
            if (btnsua.Text == "Chỉnh sửa")
                btnsua.Text = "Lưu";
            else
            {
                MemoryStream stream = new MemoryStream();
                picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                fnv.suanv2(lblmanv.Text, txtten.Text, dateTimeInput1.Value, cobgioitinh.Text, txtsdt.Text, txtdiachi.Text, stream);
                MessageBoxEx.Show("Sửa thành công !!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loadx();
                
                btnsua.Text = "Chỉnh sửa";
            }
        }

        private void lbldoimk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmDoiMatKhau frm = new frmDoiMatKhau();
            frm.manv = manv;
            frm.matkhaucu = matkhau;
            frm.ShowDialog();
        }

    }
}
